package com.ipet.app.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.ipet.app.common.App;


public class PreferenceManager {
    private static SharedPreferences mSharedPreferences;
    private static Editor mEditor;

    public final static String PREFERENCES_FILE_NAME = "client_config.pref";
    public final static int PREFERENCES_MODE_PRIVATE = Context.MODE_PRIVATE;


    private static SharedPreferences createSharedPreferences() {
        return mSharedPreferences = App.getContext().getSharedPreferences(PREFERENCES_FILE_NAME, PREFERENCES_MODE_PRIVATE);
    }

    public static SharedPreferences getMySharedPreferences() {
        if (mSharedPreferences == null) {
            mSharedPreferences = createSharedPreferences();
        }
        return mSharedPreferences;
    }

    public static void removeKey(String key) {
        if (mSharedPreferences == null) {
            mSharedPreferences = createSharedPreferences();
        }
        mEditor = mSharedPreferences.edit();
        mEditor.remove(key);
        mEditor.apply();
    }

    public static void removeAll() {
        if (mSharedPreferences == null) {
            mSharedPreferences = createSharedPreferences();
        }
        mEditor = mSharedPreferences.edit();
        mEditor.clear();
        mEditor.apply();
    }

    public static void commitString(String key, String value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = createSharedPreferences();
        }
        mEditor = mSharedPreferences.edit();
        mEditor.putString(key, value);
        mEditor.apply();
    }

    public static String getString(String key, String failValue) {
        if (mSharedPreferences == null) {
            mSharedPreferences = createSharedPreferences();
        }
        return mSharedPreferences.getString(key, failValue);
    }

    public static void commitInt(String key, int value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = createSharedPreferences();
        }
        mEditor = mSharedPreferences.edit();
        mEditor.putInt(key, value);
        mEditor.apply();
    }

    public static int getInt(String key, int failValue) {
        if (mSharedPreferences == null) {
            mSharedPreferences = createSharedPreferences();
        }
        return mSharedPreferences.getInt(key, failValue);
    }

    public static void commitLong(String key, long value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = createSharedPreferences();
        }
        mEditor = mSharedPreferences.edit();
        mEditor.putLong(key, value);
        mEditor.apply();
    }

    public static long getLong(String key, long failValue) {
        if (mSharedPreferences == null) {
            mSharedPreferences = createSharedPreferences();
        }
        return mSharedPreferences.getLong(key, failValue);
    }

    public static void commitBoolean(String key, boolean value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = createSharedPreferences();
        }
        mEditor = mSharedPreferences.edit();
        mEditor.putBoolean(key, value);
        mEditor.apply();
    }

    public static Boolean getBoolean(String key, boolean failValue) {
        if (mSharedPreferences == null) {
            mSharedPreferences = createSharedPreferences();
        }
        return mSharedPreferences.getBoolean(key, failValue);
    }
}
